// pages/checkIn/customer/customer.js
import Http from '../../../utils/http.js';
const http = new Http();
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        customerList: [],
        defaultCheckedArr: [], // 默认选中的服务客户字符串数组
        checkedArr: [], // 选中的服务客户字符串数组
        ipxBottom: 0,
        loading: true,
    },

    /**
      生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.firstparty) {
            this.setData({
                checkedArr: options.firstparty.split(','),
            });
        }

        if (options.defaultFirstparty) {
            this.setData({
                defaultCheckedArr: options.defaultFirstparty.split(','),
            });
        }

        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 34,
            });
        }
        this.getCustomerList();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    checkboxChange(e) {
        let mSet = new Set(e.detail.value.concat(this.data.defaultCheckedArr));
        this.data.customerList.forEach((item, index) => {
            if (e.detail.value.includes(item.value)) {
                (item.checked = true), (item.disabled = false);
            } else {
                item.checked = false;
                // 服务客户不能超过两个
                // 默认服务客户不能被disabled
                if (
                    mSet.size >= 2 &&
                    !this.data.defaultCheckedArr.includes(item.value)
                ) {
                    item.disabled = true;
                } else {
                    item.disabled = false;
                }
            }
        });
        this.setData({
            customerList: this.data.customerList,
        });

        this.setData({
            checkedArr: e.detail.value,
        });
    },
    backTap(e) {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            firstparty: this.data.checkedArr.join(','),
        });
        wx.navigateBack({
            delta: 1, // 返回上一级页面。
        });
    },

    /**
     * 获取服务客户
     */
    getCustomerList() {
        this.setData({
            loading: true,
        });
        http.getRequest(`cjMap/getCustomerList?authId=${app.globalData.authId}`)
            .then((res) => {
                let customerList = [];
                let mSet = new Set(
                    this.data.checkedArr.concat(this.data.defaultCheckedArr)
                );
                res.data.forEach((item) => {
                    let customer = {
                        value: item,
                        checked: false,
                        disabled: false,
                    };
                    if (this.data.checkedArr.includes(item)) {
                        customer.checked = true;
                        customer.disabled = false;
                    } else {
                        customer.checked = false;
                        // 服务客户不能超过两个
                        // 默认服务客户不能被disabled
                        if (
                            mSet.size >= 2 &&
                            !this.data.defaultCheckedArr.includes(item)
                        ) {
                            customer.disabled = true;
                        } else {
                            customer.disabled = false;
                        }
                    }
                    customerList.push(customer);
                });
                this.setData({
                    customerList: customerList,
                    loading: false,
                });
            })
            .catch((err) => {
                this.setData({
                    loading: false,
                });
            });
    },
});
